package dst.ass1.jpa.listener;

import java.util.Date;
import java.util.logging.Logger;

import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import dst.ass1.jpa.model.impl.TaskWorker;

/**
 * 1.4.b. 
 * -Entity Listeners allow us to register callback methods for specied lifecycle event-
 * Provide an entity listener which sets the joined- and lastTraining-field every time a new worker gets stored and
 * overwrites the lastTraining-field every time an already existing worker entity is updated.
 */


public class WorkerListener {

	private final static Logger LOGGER = Logger.getLogger(WorkerListener.class.getName());
	
	@PrePersist
    public void onPrePersist(TaskWorker tw) {
        final Date now = new Date();
        tw.setJoinedDate(now);		//if new TaskWorker gets Persisted set the JoinedDate
        LOGGER.info("JoinedDate set in TaskWorker");
    }

    @PreUpdate
    public void onPreUpdate(TaskWorker tw) {
        final Date now = new Date();
        tw.setLastTraining(now);  //if new TaskWorker gets Updatet set the LastTraining Date
        LOGGER.info("LastTraining set in TaskWorker");
    }
	
}
